package br.com.ffm.praticas.facade;

import java.sql.SQLException;

import br.com.ffm.praticas.dao.SalesItemDAO;

public class SalesItemFACADE {
	
	private static SalesItemFACADE salesItemFacade;
	private static SalesItemDAO salesItemDAO;
	
	/**
	 * Constructor of SalesItemFACADE
	 */
	private SalesItemFACADE(){
		salesItemDAO = new SalesItemDAO();
	}
	
	/**
	 * Method that an unique instance of SalesItemFACADE
	 * @return An instance of SalesItemFACADE
	 */
	public static SalesItemFACADE getInstance(){
		if(salesItemFacade == null){
			salesItemFacade = new SalesItemFACADE();
		}
		return salesItemFacade;
	}
	
	/**
	 * Method that check if salesItem exist
	 * @param sellID The id of the sell that references the salesItem
	 * @param productID The id of the product referenced by salesItem
	 * @return If exist return true
	 * @throws SQLException
	 */
	private boolean alreadyExist(int sellID, int productID) throws SQLException{
		return salesItemDAO.getSalesItem(sellID, productID);
	}
	
	/**
	 * Method that insert a SalesItem
	 * @param sellID The sell id
	 * @param productID the product id
	 * @return If insert return true
	 * @throws SQLException
	 */
	public boolean insert(int sellID, int productID) throws SQLException{
		if(!alreadyExist(sellID, productID)){
			salesItemDAO.insert(sellID, productID);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that delete a SalesItem
	 * @param sellID The sell id
	 * @param productID The product id
	 * @return If delete returns true
	 * @throws SQLException 
	 */
	public boolean delete(int sellID,int productID) throws SQLException{
		if(alreadyExist(sellID, productID)){
			salesItemDAO.delete(sellID, productID);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that returns the number of products
	 * @param sellId The sell id
	 * @return The number of products
	 * @throws SQLException
	 */
	public int count(int sellId) throws SQLException{
		return salesItemDAO.getProducts(sellId).size();
	}

}
